package com.berchen.utils;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.berchen.castans.ConstansUtils;
import org.apache.log4j.Logger;
import java.io.InputStream;
import java.time.LocalDate;

public class OssUtils {
    private static final Logger logger = Logger.getLogger(OssUtils.class);

    private static final String PRES_PATH = "https://" + ConstansUtils.OSS_BUKETNAME + "." + ConstansUtils.OSS_ENDPOINT + "/";

    private OssUtils() {
    }

    private static OSSClient ossClient;
    private static OSS oss;

    static {
//        ossClient = new OSSClient(ConstansUtils.OSS_ENDPOINT,ConstansUtils.OSS_ACCESSKEYID,ConstansUtils.OSS_ACCESSKEYSECRET);
//        // 判断仓库是否存在
//        boolean result = ossClient.doesBucketExist(ConstansUtils.OSS_BUKETNAME);
//        if(!result){
//            // 创建仓库
//            ossClient.createBucket(ConstansUtils.OSS_BUKETNAME);
//        }
        oss = new OSSClientBuilder().build(ConstansUtils.OSS_ENDPOINT, ConstansUtils.OSS_ACCESSKEYID, ConstansUtils.OSS_ACCESSKEYSECRET);

    }

    /**
     * 上传文件到阿里云上面
     *
     * @param presPath
     * @param filename
     * @param type
     * @param in
     * @return
     */
    public static String uploadFile(String presPath, String filename, String type, InputStream in) throws Exception {

        LocalDate now = LocalDate.now();
        String path = now.getYear() + "/" + now.getMonthValue() + "/" + presPath + "/" + filename + "." + type;

        // 解决阿里云上传文件不可以预览问题
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType(getcontentType(getcontentType(path)));
        oss.putObject(ConstansUtils.OSS_BUKETNAME, path, in,objectMetadata);
        return PRES_PATH + path;
    }

    /**
     * 替换文件上传 路径不变
     *
     * @param url
     * @param in
     * @return
     */
    public static String updateFile(String url, InputStream in) throws Exception {

        String path = url.replace(PRES_PATH, "");
        // 删除文件
        oss.deleteObject(ConstansUtils.OSS_BUKETNAME, path);

        // 解决阿里云上传文件不可以预览问题
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType(getcontentType(getcontentType(path)));
        // 同路径上传文件
        oss.putObject(ConstansUtils.OSS_BUKETNAME, path, in,objectMetadata);
        return url;
    }

    /**
     * 删除文件
     * @param url
     * @return
     * @throws Exception
     */
    public static void deleteFile(String url)throws Exception {
        String path = url.replace(PRES_PATH, "");
        // 删除文件
        oss.deleteObject(ConstansUtils.OSS_BUKETNAME, path);
    }

    /**
     * 获取文件类型
     * @param FilenameExtension
     * @return
     */
    private static String getcontentType(String FilenameExtension) {
        if (FilenameExtension.equalsIgnoreCase(".bmp")) {
            return "image/bmp";
        }
        if (FilenameExtension.equalsIgnoreCase(".gif")) {
            return "image/gif";
        }
        if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
                FilenameExtension.equalsIgnoreCase(".jpg") ||
                FilenameExtension.equalsIgnoreCase(".png")) {
            return "image/jpg";
        }
        if (FilenameExtension.equalsIgnoreCase(".html")) {
            return "text/html";
        }
        if (FilenameExtension.equalsIgnoreCase(".txt")) {
            return "text/plain";
        }
        if (FilenameExtension.equalsIgnoreCase(".vsd")) {
            return "application/vnd.visio";
        }
        if (FilenameExtension.equalsIgnoreCase(".pptx") ||
                FilenameExtension.equalsIgnoreCase(".ppt")) {
            return "application/vnd.ms-powerpoint";
        }
        if (FilenameExtension.equalsIgnoreCase(".docx") ||
                FilenameExtension.equalsIgnoreCase(".doc")) {
            return "application/msword";
        }
        if (FilenameExtension.equalsIgnoreCase(".xml")) {
            return "text/xml";
        }
        return "image/jpg";
    }
}
